﻿using e_commerce.BLL.Manager.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using e_commerce.BLL.Data;

namespace e_commerce.BLL.Services.Client
{
    public class ClientService : IUserClientApplicationService,IDisposable
    {
        private commerceDbContext _dbContext;
        public ClientService()
        {
            _dbContext = new commerceDbContext();
        }

        public void Dispose()
        {
            _dbContext.Dispose();
        }

        public void SaveChanges()
        {
            _dbContext.SaveChanges();
        }

        public void ModifyDeliveryInfo(DeliveryInfo deliveryInfo)
        {
            var newDeliveryInfo = _dbContext.DeliveryInfo.SingleOrDefault(d => d.ClientId == deliveryInfo.ClientId);
            newDeliveryInfo = deliveryInfo;
            SaveChanges();
        }

        public void ModifyPasswordByUserId(int userId, string password)
        {
            var user = _dbContext.User.SingleOrDefault(u => u.UserId == userId);
            if(user == null)
            {
                throw new Exception("不存在该用户");
            }
            user.Password = password;
            SaveChanges();
        }

        public void ModifyUserInfo(User user)
        {
            var OldUser = _dbContext.User.SingleOrDefault(u => u.UserId == user.UserId);
            OldUser = user;
            SaveChanges();
        }
    }
}
